package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: tantan
 * @date: 2020/4/19-2:17
 * @Version: 1.0.0
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    MenuMapper menuMapper;
    private List<Integer> allMenuIds = null;
    @Override
    public List<Menu> getMenuList(String username) {
        Integer id = userMapper.findByUserName(username).getId();
        //获取该用户的所有菜单的id集合
        allMenuIds = menuMapper.getAllMenuIds(id);
        //获取一级菜单
        List<Menu> menus = menuMapper.getMenuList(id);
        //二级
        if (CollectionUtil.isNotEmpty(menus)) {
            for (Menu menu : menus) {
                //List<Menu> childrenMenuList = menuMapper.getChildrenMenuList(menu.getId());
                    List<Menu> childrenMenuList = getUserChildrenMenuList(menu);
                    menu.setChildren(childrenMenuList);

            }
        }
        return menus;
    }
    @Override
    public List<Menu> findAll() {
        //获取所有一级菜单
        List<Menu> menus = menuMapper.findAll1stMenu();
        if (CollectionUtil.isNotEmpty(menus)) {
            for (Menu menu : menus) {
                List<Menu> childrenMenuList = getChildrenMenuList(menu);
                menu.setChildren(childrenMenuList);
            }
        }
        return menus;
    }

    private List<Menu> getChildrenMenuList(Menu menu) {
        List<Menu> childrenMenuList = menuMapper.getChildrenMenuList(menu.getId());
        if (CollectionUtil.isNotEmpty(childrenMenuList)) {
            for (Menu childernMenu : childrenMenuList) {
                childernMenu.setChildren(getChildrenMenuList(childernMenu));
            }
        }
        return childrenMenuList;
    }

    private List<Menu> getUserChildrenMenuList(Menu menu) {
        List<Menu> childrenMenuList = menuMapper.getChildrenMenuList(menu.getId());
        List<Menu> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(childrenMenuList)) {
            for (Menu childernMenu : childrenMenuList) {
                if (CollectionUtil.contains(allMenuIds, childernMenu.getId())) {
                    childernMenu.setChildren(getUserChildrenMenuList(childernMenu));
                } else {
                    list.add(childernMenu);
                }
            }
        }
        childrenMenuList.removeAll(list);
        return childrenMenuList;
    }


    @Override
    public List<Menu> getMenus(Integer id) {
        List<Menu> menus = menuMapper.getMenuByParentId(id);
        getChildrenMenu(menus);
        return menus;
    }

    @Override
    public void add(Menu menu) {
        menu.setPath(UUID.randomUUID().toString());
        menuMapper.add(menu);
    }

    private void getChildrenMenu(List<Menu>menus){
        if (CollectionUtil.isNotEmpty(menus)){
            for (Menu menu : menus) {
                List<Menu>cmenus=menuMapper.getMenuByParentId(menu.getId());
                menu.setChildren(cmenus);
                getChildrenMenu(cmenus);//更改查询多级菜单
            }
        }
    }
}
